package com.jsgt.greekfire.ui.trading.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.core.content.ContextCompat
import com.fhx.core.base.BaseAdapter
import com.fhx.core.base.BaseViewHolder
import com.jsgt.greekfire.R
import com.jsgt.greekfire.bean.UserEntrustItem
import java.text.DecimalFormat

class UserEntrustAdapter : BaseAdapter<UserEntrustItem>() {
    var onCancelClickListener: ((UserEntrustItem) -> Unit)? = null
    val df = DecimalFormat("######0.0000")

    override fun provideViewHolder(parent: ViewGroup, viewType: Int): BaseViewHolder? {
        val view =
            LayoutInflater.from(parent.context)
                .inflate(R.layout.adapter_trust_layout, parent, false)
        return VH(view)
    }

    inner class VH(itemView: View) : BaseViewHolder(itemView) {
        var tv_1: TextView? = itemView.findViewById(R.id.trust_type)
        var tv_2: TextView? = itemView.findViewById(R.id.trust_time)
        var tv_3: TextView? = itemView.findViewById(R.id.trust_back)
        var tv_4: TextView? = itemView.findViewById(R.id.trust_price)
        var tv_5: TextView? = itemView.findViewById(R.id.trust_num)
        var tv_6: TextView? = itemView.findViewById(R.id.trust_done)
        var tvDelKey: TextView? = itemView.findViewById(R.id.tvDelKey)

        fun bindData(bean: UserEntrustItem) {
            if (bean.dealType == 1) {
                tv_1?.text = tv_1?.context!!.getString(R.string.buying)
                tv_1?.setTextColor(ContextCompat.getColor(tv_1?.context!!, R.color.trading_buy))
            } else {
                tv_1?.text = tv_1?.context!!.getString(R.string.sale)
                tv_1?.setTextColor(ContextCompat.getColor(tv_1?.context!!, R.color.trading_sell))
            }
            tv_2?.text = bean.createTime
            if (bean.dealStatus == 1 || bean.dealStatus == 2) {
                tv_3?.visibility = View.VISIBLE
                tv_3?.setOnClickListener {
                    if (onCancelClickListener != null) {
                        onCancelClickListener?.invoke(bean)
                    }
                }
            } else {
                tv_3?.visibility = View.INVISIBLE
            }
            tv_4?.text = bean.dealAmount.toString()
            tv_5?.text = bean.dealNum.toString()
            tv_6?.text = df.format(bean.dealNum - bean.residueNum).replace(".0000", "")
            tvDelKey?.text = bean.coinCode + "/" + bean.fixedCode
        }
    }

    override fun onBindViewHolder(holder: BaseViewHolder, position: Int) {
        val viewHolder = holder as UserEntrustAdapter.VH
        viewHolder.bindData(dataList()[position])
    }
}